﻿function __MVC_To_JQuery_Converter(value) {
    switch (value) {
        case "regularExpression":
            value = "regex";
            break;

        case "stringLength":
            value = "maxlength";
            break;
    }
    return value;

}

$.validator.addMethod("negateValidator", function (value, par, properties) {

    var rules = __MVC_CreateRulesForField({ ValidationRules: [properties.innerValidator] });
    var method = __MVC_To_JQuery_Converter(properties.innerValidator.ValidationType);
    var rule = { method: method, parameters: rules[method] };
    var result = $.validator.methods[method].call(this, value, par, rule.parameters);
    if (result === true || result === false) {
        return !result;
    }
    return result;
}, "Your value is incorrect");


$.validator.addMethod("orComposite", function (value, par, properties) {
    var innerValidators = properties.innerValidators;

    var overalrResult = true;
    for (var i = 0; i < innerValidators.length; i++) {
        var validators = innerValidators[i];
        var innerValidatorsValues = true;
        for (var k = 0; k < validators.length; k++) {
            var innerValidator = validators[k];
            var rules = __MVC_CreateRulesForField({ ValidationRules: [innerValidator] });
            var method = __MVC_To_JQuery_Converter(innerValidator.ValidationType);
            var rule = { method: method, parameters: rules[method] };
            var result = $.validator.methods[method].call(this, value, par, rule.parameters);
            if (!(result === true || result === false || result === null)) {
                return result;
            }
            innerValidatorsValues = innerValidatorsValues && result;
        }

        if (i == 0) {
            overalrResult = innerValidatorsValues;
            firstEntrance = false;
        }
        else {
            overalrResult = overalrResult || innerValidatorsValues;
        }

    }
    return overalrResult;
}, "Your value is incorrect");

$.validator.addMethod("andComposite", function (value, par, properties) {
    var innerValidators = properties.innerValidators;

    var overalrResult = true;
    for (var i = 0; i < innerValidators.length; i++) {
        var validators = innerValidators[i];
        var innerValidatorsValues = true;
        for (var k = 0; k < validators.length; k++) {
            var innerValidator = validators[k];
            var rules = __MVC_CreateRulesForField({ ValidationRules: [innerValidator] });
            var method = __MVC_To_JQuery_Converter(innerValidator.ValidationType);
            var rule = { method: method, parameters: rules[method] };
            var result = $.validator.methods[method].call(this, value, par, rule.parameters);
            if (!(result === true || result === false || result === null)) {
                return result;
            }
            innerValidatorsValues = innerValidatorsValues && result;
        }

        if (i == 0) {
            overalrResult = innerValidatorsValues;
        }
        else {
            overalrResult = overalrResult && innerValidatorsValues;
        }

    }
    return overalrResult;
}, "Your value is incorrect");

$.validator.addMethod("checkLengthCorrect", function (value, element, param) {
    var minimumCorrect = this.getLength($.trim(value), element) >= param.minimumLength;
    var maximumCorrect = this.getLength($.trim(value), element) <= param.maximumLength;
    var result = minimumCorrect && maximumCorrect;
    return result;
}, "Your value is incorrect");

$.validator.addMethod("containsCharactersValidator", function (value, element, param) {

    if (param.containsCharecters == "Any") {
        for (var i = 0; i < param.characters.length; i++) {
            if (value.indexOf(param.characters[i]) >= 0) {
                return true;
            }
        }
        return false;
    }
    if (param.containsCharecters == "All") {
        for (var i = 0; i < param.characters.length; i++) {
            if (value.indexOf(param.characters[i]) < 0) {
                return false;
            }
        }
        return true;
    }
}, "Your value is incorrect");

$.validator.addMethod("domainValidator", function (value, element, param) {
    if ($.inArray(value, param.domain) >= 0) {
        return true;
    }
    return false;
}, "Your value is incorrect");


$.validator.addMethod("rangeValidatorComplex", function (value, element, param) {
    var upperBound = param.upperBound;
    var lowerBound = param.lowerBound;

    value = $.trim(value);
    if (
           /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value) //number
           ||
           /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value) //numberDE
            ) {
        value = parseFloat(value.replace(",", "."));
        lowerBound = parseFloat(lowerBound.replace(",", "."));
        upperBound = parseFloat(upperBound.replace(",", "."));
    }
    else if (value.length == 1) {
        value = value.charCodeAt(0);
        lowerBound = lowerBound.charCodeAt(0);
        upperBound = upperBound.charCodeAt(0);
    }
    else {
        return false;
    }

    var arrayOfSigns = [" || true || ", "<=", "<", " || true || ", ">=", ">"];

    var stringToEval = "var a = Boolean(" + lowerBound + arrayOfSigns[param.lowerBoundType] + value + ");" +
                               "var b = Boolean(" + upperBound + arrayOfSigns[param.upperBoundType] + value + ");"

    eval(stringToEval);
    return a && b;


}, "Your value is incorrect");